package com.example.demo.service.impls;

import com.example.demo.domain.User;
import com.example.demo.repository.UserJpaRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.interfaces.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class UserService implements IUserService {
    @Autowired
    private UserJpaRepository userJpaRepository;

    @Autowired
    private UserRepository userRepository;

    public UserService() {
    }

    @Override
    public List<User> findAll() {
        return userJpaRepository.findAll();
    }

    @Override
    public void saveUser(User book) {
        userJpaRepository.save(book);
    }

    @Cacheable("users")
    @Override
    public User findOne(long id) {
        System.out.println("Cached pages");

        Optional<User> result = userJpaRepository.findById(id);

        return result.get();
    }

    @Override
    public void delete(long id) {
        userJpaRepository.deleteById(id);
    }

    @Override
    public List<User> findByName(String name) {
        List<User> userList1 = userRepository.findByName1(name);
        List<User> userList2 = userRepository.findByName2(name);
        List<User> userList3 = userRepository.findByNameAndAddress(name, "3");
        System.out.println("userList1:" + userList1);
        System.out.println("userList2:" + userList2);
        System.out.println("userList3:" + userList3);

        return userRepository.findByName(name);
    }

    /**
     * 更新用户信息
     *

     */
    @Override
    public void updateUser(long id, String address) {
        userRepository.updateUser(id, address);
    }

}
